#include <sstream>
#include <stdio.h>
#include <iostream>
#include <chrono>


float str2float_1(const std::string& str)
{
    float ret = 0.0f;
    if (!str.empty()) {
        sscanf(str.c_str(), "%f", &ret);
    }  
    return ret;
}
float str2float_2(const std::string& str)
{
    float ret = 0.0f;
    if (!str.empty()) {
        std::istringstream iss(str);
        iss >> ret;
    }    
    return ret;
}

float str2float_3(const std::string& str)
{
    float ret = 0.0f;
    if (!str.empty()) {
       ret = std::stof(str.c_str());
    }    
    return ret;
}

int main (int argc, char* argv[])
{
    int loop_cnt = 200;
    if (argc > 1) {
        loop_cnt = std::stoi(argv[1]);
        if (loop_cnt < 1) {
            loop_cnt = 200;
        }
    }


    std::string str = "0.999999";
    std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now();
    for (int i=0; i<loop_cnt; ++i) {
        str2float_1(str);
    }
    std::chrono::steady_clock::time_point end_time = std::chrono::steady_clock::now();
    std::cout << "str2float_1 using sscanf " << loop_cnt << " times costs: " << std::chrono::duration_cast<std::chrono::milliseconds>(
        end_time - start_time).count() << std::endl;

    start_time = std::chrono::steady_clock::now();
    for (int i=0; i<loop_cnt; ++i) {
        str2float_2(str);
    }
    end_time = std::chrono::steady_clock::now();
    std::cout << "str2float_1 using sstream " << loop_cnt << " times costs: " << std::chrono::duration_cast<std::chrono::milliseconds>(
        end_time - start_time).count() << std::endl;

    start_time = std::chrono::steady_clock::now();
    for (int i=0; i<loop_cnt; ++i) {
        str2float_3(str);
    }
    end_time = std::chrono::steady_clock::now();
    std::cout << "str2float_1 using std::stof " << loop_cnt << " times costs: " << std::chrono::duration_cast<std::chrono::milliseconds>(
        end_time - start_time).count() << std::endl;

    return 0;
}